/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.om.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.poll.om.entity.BaseOmDataEntity;
import com.hopes.bizz.poll.om.entity.PollMonthEntity;
import com.hopes.bizz.poll.om.entity.PollYearEntity;
import com.hopes.bizz.poll.om.logic.MonitorDataStatisticLogic;
import com.hopes.bizz.poll.om.mapper.PollMonthMapper;
import com.hopes.bizz.poll.om.mapper.PollYearMapper;
import com.hopes.bizz.poll.om.service.PollYearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 年监控数据历史
 *
 * @author pig
 * @date 2023-05-23 14:05:47
 */
@Service
@Slf4j
public class PollYearServiceImpl extends ServiceImpl<PollYearMapper, PollYearEntity> implements PollYearService {
    @Autowired
    private PollMonthMapper pollMonthMapper;

    @Override
    public void batchStatisticMonitorData(Date beginTime, Date endTime) {
        long begin = System.currentTimeMillis();

        LambdaQueryWrapper<PollMonthEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(PollMonthEntity::getDataTime, beginTime);
        wrapper.le(PollMonthEntity::getDataTime, endTime);
        List<PollMonthEntity> list = pollMonthMapper.selectList(wrapper);

        List<BaseOmDataEntity> dataList = MonitorDataStatisticLogic.doStatistic(beginTime, list);
        if (CollectionUtil.isNotEmpty(dataList)) {
            this.getBaseMapper().deleteData(beginTime);
            this.saveBatch(dataList.stream().map(it -> {
                PollYearEntity entity = new PollYearEntity();
                BeanUtils.copyProperties(it, entity);
                return entity;
            }).collect(Collectors.toList()));
        }
        long end = System.currentTimeMillis();
        log.trace("统计年排放数据 ===> beginTime={}, endTime={}, 时长(ms)={}", beginTime, endTime, end - begin);
    }
}
